<template>
  <div class="container">
    <el-card class="box-card">
      <!-- 新增目录弹框 -->
      <el-dialog
        title="新增目录"
        :visible.sync="addDialogVisible"
        width="30%"
        @closed="addCancel()"
      >
        <el-form
          :rules="addRules"
          label-width="80px"
          :model="addRuleForm"
          ref="addRuleForm"
        >
          <el-form-item label="所属学科" v-if="!$route.query.id">
            <el-select
              v-model="addRuleForm.subjectID"
              placeholder="请选择"
              class="addSelect"
            >
              <el-option
                v-for="item in tableData"
                :key="item.id"
                :value="item.subjectID"
                :label="item.subjectName"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="directoryName">
            <el-input v-model="addRuleForm.directoryName"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addSubmit()">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 新增目录弹框 -->

      <!-- 头部搜索 -->
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="目录名称" class="catalog">
          <el-input
            v-model="catalogQuery.directoryName"
            size="small "
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="catalogQuery.state"
            placeholder="请选择"
            size="small "
          >
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="clearInput">清除</el-button>
          <el-button type="primary" @click="search" size="small"
            >搜索</el-button
          >
        </el-form-item>
        <el-form-item class="addCatalog">
          <el-button
            type="text"
            icon="el-icon-back"
            @click="$router.push('/subjects/list')"
            v-if="subject.id && subject.name"
            >返回学科</el-button
          >
          <el-button
            type="success"
            icon="el-icon-edit"
            size="small"
            @click="addDialogVisible = true"
            >新增目录</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 头部搜索 -->

      <!-- 消息提示 -->
      <el-alert type="info" show-icon :closable="false"
        >数据一共 {{ listTotals }} 条
      </el-alert>
      <!-- 消息提示 -->

      <!-- 表单数据 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="序号" type="index"> </el-table-column>
        <el-table-column label="所属学科" prop="subjectName"> </el-table-column>
        <el-table-column label="目录名称" prop="directoryName">
        </el-table-column>
        <el-table-column label="创建者" prop="username"> </el-table-column>
        <el-table-column
          label="创建日期"
          prop="addDate"
          :formatter="formatDate"
        >
        </el-table-column>
        <el-table-column label="面试数量" prop="totals"> </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.state === 1">已启用</span>
            <span v-else-if="scope.row.state === 0">已禁用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" prop="subjectName">
          <template slot-scope="scope">
            <el-button
              type="text"
              v-if="scope.row.state === 0"
              @click="changeTable(scope.row)"
              >启用</el-button
            >
            <el-button
              type="text"
              v-if="scope.row.state === 1"
              @click="changeTable(scope.row)"
              >禁用</el-button
            >
            <el-button
              type="text"
              :disabled="scope.row.state == 0 ? false : true"
              @click="changed(scope.row)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="scope.row.state == 0 ? false : true"
              @click="deleted(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 表单数据 -->

      <!-- 分页按钮区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="catalogQuery.page"
        :page-sizes="[4, 6, 8, 10]"
        :page-size="catalogQuery.pagesize"
        layout="  total,prev, pager, next, sizes,jumper"
        :total="listTotals"
      >
      </el-pagination>
      <!-- 分页按钮区域 -->

      <!-- 修改弹框 -->
      <el-dialog
        title="修改目录"
        :visible.sync="changeDialogVisible"
        width="30%"
      >
        <el-form
          :rules="addRules"
          :model="changeData"
          label-width="80px"
          ref="changeData"
        >
          <el-form-item label="所属学科">
            <el-select
              v-model="changeData.cgSubjectName"
              placeholder="请选择"
              class="addSelect"
            >
              <el-option
                v-for="item in tableData"
                :key="item.id"
                :value="item.subjectID"
                :label="item.subjectName"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="目录名称" prop="cgDrectoryName">
            <el-input v-model="changeData.cgDrectoryName"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="changeDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="changeSubmit()">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 修改弹框 -->
    </el-card>
  </div>
</template>

<script>
import {
  list,
  add,
  changeState,
  update,
  remove
} from "../../api/hmmm/directorys";

export default {
  name: "aaaaa",
  data() {
    return {
      // 表单数据
      tableData: [],

      // 获取数据请求参数
      catalogQuery: {
        page: 1,
        pagesize: 10,
        subjectID: null,
        directoryName: "",
        state: null
      },

      // 获取所有数据的数量
      listTotals: 0,

      // 控制新增弹框弹出
      addDialogVisible: false,

      //  新增目录数据
      addRuleForm: {
        subjectID: +this.$route.query.id || "",
        directoryName: ""
      },

      // 修改数据
      changeData: {
        cgSubjectName: "",
        cgDrectoryName: "",
        cgID: null
      },

      // 新增修改目录表单验证
      addRules: {
        directoryName: [
          { required: true, message: "请输入目录名称", trigger: "blur" },
          { min: 1, max: 7, message: "长度在 1 到 7 个字符", trigger: "blur" }
        ],
        cgDrectoryName: [
          { required: true, message: "请输入目录名称", trigger: "blur" },
          { min: 1, max: 7, message: "长度在 1 到 7 个字符", trigger: "blur" }
        ]
      },

      // 修改按钮取消或确定
      changeDialogVisible: false
    };
  },

  created() {
    this.catalogQuery.subjectID = this.$route.query.id || null;
    this.getCatalogList();
  },

  computed: {
    subject() {
      return this.$route.query || {};
    }
  },

  methods: {
    // 搜索
    async search() {
      if (
        this.catalogQuery.directoryName == "" &&
        this.catalogQuery.state == null
      ) {
        return this.$message.error("请输入搜索内容");
      }
      this.getCatalogList();
    },

    // 清除
    clearInput() {
      this.getCatalogList();
      this.catalogQuery.state = null;
      this.catalogQuery.directoryName = "";
    },

    // 点击确定提交数据
    async addSubmit() {
      try {
        await add(this.addRuleForm);
        this.getCatalogList();
        this.$message.success("添加数据成功");
      } catch (err) {
        this.$message.error("添加数据失败");
      }
      this.addDialogVisible = false;
    },

    // 关闭对话框 重置表单
    addCancel() {
      this.addRuleForm.subjectID = "";
      this.$refs.addRuleForm.resetFields();
    },

    // 获取目录列表
    async getCatalogList() {
      const res = await list(this.catalogQuery);
      if (res.status !== 200) return this.$message.error("获取数据失败");
      this.tableData = res.data.items;
      this.listTotals = res.data.counts;
    },

    // 点击切换启用或禁用
    async changeTable(data) {
      const res = await changeState({
        id: data.id,
        state: data.state === 1 ? 0 : 1
      });
      if (res.status !== 200) return this.$message.error("操作失败");
      data.state = data.state === 1 ? 0 : 1;
      this.$message.success("操作成功");
    },

    // 修改弹框
    changed(data) {
      this.changeDialogVisible = true;

      this.changeData.cgDrectoryName = data.directoryName;
      this.changeData.cgSubjectName = data.subjectName;

      this.changeData.cgID = data.id;
      this.changeData.cgSubjectID = data.subjectID;
    },

    // 修改的提交-确定按钮
    async changeSubmit() {
      const res = await update({
        id: this.changeData.cgID,
        subjectID: this.changeData.cgSubjectID,
        directoryName: this.changeData.cgDrectoryName
      });

      if (res.status !== 200) return this.$message.error("修改失败");
      this.getCatalogList();

      this.changeDialogVisible = false;

      this.$message.success("修改成功");
    },

    // 删除功能
    async deleted(data) {
      const res = await remove(data);
      if (res.status !== 200) return this.$message.error("删除失败");
      this.getCatalogList();
      this.$message.success("删除成功");
    },

    // 每页可以展示多少条数据
    handleSizeChange(val) {
      this.catalogQuery.pagesize = val;
      this.getCatalogList();
    },

    // 输入页码数跳转
    handleCurrentChange(val) {
      this.catalogQuery.page = val;
      this.getCatalogList();
    }
  }
};
</script>

<style lang="less" scoped>
.container {
  padding: 10px 10px;
  .el-card {
    height: 100%;
  }
  .catalog {
    margin-right: 50px;
  }
  .addCatalog {
    float: right;
  }
  /deep/ .el-alert {
    height: 35px;
    margin-bottom: 15px;
    .el-alert__icon {
      font-size: 16px;
      width: 16px;
    }
    .el-alert__description {
      font-size: 13px;
      margin: 0;
    }
  }
  .el-pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
  .addSelect {
    width: 100%;
  }
}
</style>
